<template>
	<view class="enterprise_list_layout">
		<z-paging ref="paging" v-model="list" @query="queryList" class="container" show-refresher-when-reload>
			<uni-swipe-action ref="swipe_action">
				<uni-swipe-action-item :right-options="options" v-for="(item,index) in list" :key="item._id"
					class="swipe_item_container" @click="onClick($event, item)">
					<view class="content" @click="enterprise_click(item)">
						<view class="name">企业名称：{{item.name}}</view>
						<view class="contacts">法定代表人：{{item.contacts}}</view>
						<view class="status">经营状态：{{enterprise_text_value(item.status)}}</view>
					</view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</z-paging>
		<uni-fab horizontal="right" @fabClick="fabClick"></uni-fab>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'

	const list = ref([])
	const paging = ref(null)
	const swipe_action = ref(null)
	const enterprise = uniCloud.importObject('opendb-enterprise', {
		customUI: true
	})

	import {
		enterprise_text_value
	} from '@/utils/enterprise.js'

	const options = ref([{
			text: "编辑",
			style: {
				backgroundColor: "#10ae18",
				fontSize: '13px'
			}
		},
		{
			text: "删除",
			style: {
				backgroundColor: '#E40C39',
				fontSize: '13px'
			}
		}
	])

	const queryList = async (pageNo, pageSize) => {
		let res = await enterprise.opendb_enterprise_listByPage(pageNo, pageSize)
		console.log('企业列表', res);
		if (res.errCode != 0) {
			paging.value.complete(false)
			return
		}
		paging.value.complete(res.data)
	}

	function onClick(e, params) {
		if (e.index == 0) {
			uni.navigateTo({
				url: '/pages_mine/enterprise/enterprise_add?id=' + params._id
			})
		} else if (e.index == 1) {
			enterprise_delete_action(params)
		}
		swipe_action.value.closeAll()
	}

	function fabClick() {
		uni.navigateTo({
			url: '/pages_mine/enterprise/enterprise_add'
		})
	}

	function enterprise_click(params) {
		uni.navigateTo({
			url: '/pages_mine/enterprise/enterprise_info?id=' + params._id
		})
	}

	/* 删除事件 */
	function enterprise_delete_action(params) {
		uni.showModal({
			title: '温馨提醒',
			content: `确定删除"${params.name}"？`,
			success: (res) => {
				if (!res.confirm) {
					return
				}
				enterprise_delete(params)
			}
		})
	}

	/* 删除 */
	async function enterprise_delete(params) {
		uni.showLoading()
		let result = await enterprise.opendb_enterprise_delete(params._id)
		uni.hideLoading()
		let flag = result.errCode == 0
		uni.showToast({
			title: flag ? "删除成功" : result.errMsg,
			icon: flag ? 'success' : 'fail'
		})
		if (flag) {
			paging.value.refresh()
		}
	}

	uni.$on('enterprise_change', () => {
		if (paging.value) {
			paging.value.refresh()
		}
	})
</script>

<style lang="scss" scoped>
	.enterprise_list_layout {
		.container {
			.swipe_item_container {
				margin-top: 20rpx;

				.content {
					margin: 0 20rpx;
					padding: 26rpx 20rpx;
					background-color: $me-color-background;
					border-radius: 8rpx;

					.name {
						font-size: 28rpx;
						color: $me-color-gray-1;
					}

					.contacts {
						margin-top: 10rpx;
						font-size: 28rpx;
						color: $me-color-gray-1;
					}

					.status {
						margin-top: 10rpx;
						font-size: 28rpx;
						color: $me-color-gray-1;
					}
				}
			}
		}
	}
</style>